package com.ruoyi.operator.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.operator.domain.OptShopMember;
import com.ruoyi.operator.mapper.OptMemberOrderMapper;
import com.ruoyi.operator.mapper.OptShopMemberMapper;
import com.ruoyi.operator.service.IOptMemberOrderDetailService;
import com.ruoyi.operator.service.IOptShopMemberService;
import com.ruoyi.operator.service.IOptShopService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.operator.domain.OptMemberOrder;
import com.ruoyi.operator.service.IOptMemberOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员收支记录Controller
 * 
 * @date 2021-01-27
 */
@Controller
@RequestMapping("/operator/order")
public class OptMemberOrderController extends BaseController
{
    private String prefix = "operator/order";

    @Autowired
    private IOptMemberOrderService optMemberOrderService;

    @Autowired
    private IOptShopMemberService optShopMemberService;

    @Autowired
    private IOptShopService optShopService;


    @Autowired
    private OptMemberOrderMapper optMemberOrderMapper;
    @Autowired
    private OptShopMemberMapper optShopMemberMapper;



    @RequiresPermissions("operator:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询会员收支记录列表
     */
    @RequiresPermissions("operator:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OptMemberOrder optMemberOrder)
    {
        startPage();
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if(!(currentUser.getDeptId() == 100)){ optMemberOrder.setDeptId(currentUser.getDeptId()); }
        List<OptMemberOrder> list = optMemberOrderService.selectOptMemberOrderList(optMemberOrder);
        return getDataTable(list);
    }

    /**
     * 导出会员收支记录列表
     */
    @RequiresPermissions("operator:order:export")
    @Log(title = "会员收支记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OptMemberOrder optMemberOrder)
    {
        List<OptMemberOrder> list = optMemberOrderService.selectOptMemberOrderList(optMemberOrder);
        ExcelUtil<OptMemberOrder> util = new ExcelUtil<OptMemberOrder>(OptMemberOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 充值
     */
    @GetMapping(value = { "/recharge", "/recharge/{memberCardNumber}" })
    public String recharge(@PathVariable(value = "memberCardNumber", required = false) String memberCardNumber, ModelMap mmap)
    {
        if (StringUtils.isNotEmpty(memberCardNumber))
        {
            mmap.put("optMemberOrder", optShopMemberService.selectMemberByCardNumber(memberCardNumber));
        }
        return prefix + "/recharge";
    }
    /**
     * 会员充值
     */
    @RequiresPermissions("operator:order:recharge")
    @Log(title = "会员充值", businessType = BusinessType.INSERT)
    @PostMapping("/recharge")
    @ResponseBody
    public AjaxResult rechargeSave(OptMemberOrder optMemberOrder) {
        optShopService.checkUserAllowed(optMemberOrder.getDeptId());
        if(null==optMemberOrder.getGivingAmount()){
            optMemberOrder.setGivingAmount(new BigDecimal("0.00") ); }
        if(null==optMemberOrder.getConsumeAmount()){
            optMemberOrder.setConsumeAmount(new BigDecimal("0.00") ); }
        if(optMemberOrder.getGivingAmount().compareTo(BigDecimal.ZERO) == -1
                ||optMemberOrder.getConsumeAmount().compareTo(BigDecimal.ZERO)==-1){
            return error("金额不能为负数!");
        }
        return toAjax(optMemberOrderService.insertOptMemberOrder(optMemberOrder));
    }

    /**
     * 消费
     */
    @GetMapping(value = { "/expense", "/expense/{memberCardNumber}" })
    public String expense(@PathVariable(value = "memberCardNumber", required = false) String memberCardNumber, ModelMap mmap)
    {
        if (StringUtils.isNotEmpty(memberCardNumber))
        {
            mmap.put("optMemberOrder", optShopMemberService.selectMemberByCardNumber(memberCardNumber));
        }
        return prefix + "/expense";
    }

    /**
     * 会员消费
     */
    @RequiresPermissions("operator:order:expense")
    @Log(title = "会员消费", businessType = BusinessType.INSERT)
    @PostMapping("/expense")
    @ResponseBody
    public AjaxResult expenseSave(OptMemberOrder optMemberOrder) {
        optShopService.checkUserAllowed(optMemberOrder.getDeptId());
        OptShopMember optMemberOrderinfo = optShopMemberService.selectMemberByCardNumber(optMemberOrder.getMemberCardNumber());
        if (optMemberOrder.getConsumeAmount().compareTo(optMemberOrderinfo.getBalance()) == 1) { return  error("余额不足"); }
        return toAjax(optMemberOrderService.insertOptMemberOrderExpense(optMemberOrder));
    }


    @PostMapping("/summarizing")
    @ResponseBody
    public AjaxResult summarizing(Map<String, Object> param) throws ParseException {
        SysUser currentUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        OptMemberOrder optshopmember = new OptMemberOrder();
        OptMemberOrder optMemberOrder = new OptMemberOrder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        optshopmember.setDeptId(currentUser.getDeptId());
        optMemberOrder.setDeptId(currentUser.getDeptId());
        int membersAmount = optShopMemberService.selectMemberCountByDept(currentUser.getDeptId());
        int  orderAmount =  optMemberOrderService.selectOrderCountByDept(currentUser.getDeptId());
        List<Map<String, Object>>  sameDayMemberslist = optShopMemberMapper.getSameDayMembers(optMemberOrder);
        List<Map<String, Object>>  indentlist = optMemberOrderMapper.getdayIndent(optMemberOrder);
        int  memberscount = 0;
        int indentcount = 0;
        for (Map<String, Object> stringObjectMap : sameDayMemberslist) {
            String dbdate = (stringObjectMap.get("create_time").toString()).substring(0,10);
            String dateNowStr = sdf.format(new Date());
            if(dbdate.equals(dateNowStr)){ memberscount++; }
        }
        for (Map<String, Object> stringObjectMap : indentlist) {
            String dbdate = (stringObjectMap.get("create_time").toString()).substring(0,10);
            String dateNowStr = sdf.format(new Date());
            if(dbdate.equals(dateNowStr)){ indentcount++; }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("membersAmount", membersAmount);
        result.put("memberscount", memberscount);
        result.put("orderAmount", orderAmount);
        result.put("indentcount", indentcount);
        return AjaxResult.success("操作成功", result);
    }
}
